package com.codedef.controller;

import com.codedef.common.response.Result;
import com.codedef.entity.Seat;
import com.codedef.service.SeatService;
import com.codedef.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/seat")
public class SeatController {

    @Autowired
    SeatService seatService;

    /**
     * 通过教室的ID，获取某一个教室内所有的座位
     */
    @GetMapping("/classroom/{id}")
    public Result getSeatListByClassroomId(@PathVariable("id") Integer classroomId) {
        return Result.SUCCESS(seatService.getSeatListByClassroomId(classroomId));
    }

    /**
     * 通过教室的ID，获取某一个教室内所有的座位
     * 包含当前预约状态
     */
    @GetMapping("/classroomWithReservation/{id}")
    public Result getSeatListWithReservationByClassroomId(@PathVariable("id") Integer classroomId) {
        return Result.SUCCESS(seatService.getSeatListWithReservationByClassroomId(classroomId));
    }

    /**
     * 获取一个时间段内空间的座位列表
     */
    @PostMapping("/freeByTimePeriod")
    public Result getFreeSeatListByTimePeriod(@RequestBody Map<String, String> params) {
//        System.out.println(params);
        Date startTime = DateUtil.datetimeStrToDate(params.get("startTime"));
        Date endTime = DateUtil.datetimeStrToDate(params.get("endTime"));
        if (startTime == null || endTime == null)
            return Result.FAIL("参数不正确");

        return Result.SUCCESS(seatService.getFreeSeatListByTimePeriod(startTime, endTime));
    }


    /* ----------------------------------------- Feign ----------------------------------------- */
    /**
     * 获得当前有预约记录的座位Id的列表 Feign接口
     */
    @GetMapping("/f/nowReservationSeatIdList/{classroomId}")
    public List<Integer> getNowReservationSeatIdListByClassroomIdFeign(@PathVariable("classroomId") Integer classroomId) {
        return seatService.getNowReservationSeatIdListByClassroomId(classroomId);
    }

    /**
     * 通过教室的ID，获取某一个教室内所有的座位 Feign接口
     */
    @GetMapping("/f/classroom/{classroomId}")
    public List<Seat> getSeatListByClassroomIdFeign(@PathVariable("classroomId") Integer classroomId) {
        return seatService.getSeatListByClassroomId(classroomId);
    }

    /**
     * 查询一个座位所属的教室Abbreviation Feign接口
     */
    @GetMapping("/f/classroomAbbr/{seatId}")
    public String getClassroomAbbreviationBySeatIdFeign(@PathVariable("seatId") Integer seatId) {
        return seatService.getClassroomAbbreviationBySeatId(seatId);
    }

    /**
     * 通过Id查询一个座位
     */
    @GetMapping("/f/seat/{id}")
    public Seat getSeatByIdFeign(@PathVariable("id") Integer id) {
        return seatService.getSeatById(id);
    }

}
